/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neo.workforce.dao.impl;

import java.sql.SQLException;
import neo.workforce.dao.ProcessDAO;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author krisada
 */
public class ProcessDAOImpl implements ProcessDAO {

    private HibernateTemplate hibernateTemplate;
    private Logger log = Logger.getLogger(this.getClass());

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public neo.workforce.dao.Process find(final long id) {
        return hibernateTemplate.get(neo.workforce.dao.Process.class, new Long(id));
    }

    public Long save(neo.workforce.dao.Process process) {
        Long lid = (Long) hibernateTemplate.save(process);
        return lid;
    }

    public boolean update(neo.workforce.dao.Process process) {
        try {
            hibernateTemplate.update(process);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }

    }

    public java.util.List findByName(final String processName) {
        StringBuilder buf = new StringBuilder();
        buf.append("FROM neo.workforce.dao.Process o");
        if (processName != null) {
            buf.append(" WHERE o.name like '%").append(processName).append("%' desc o.createDate");
        }
        log.info(buf.toString());
        java.util.List ls = hibernateTemplate.find(buf.toString());
        return ls;
    }

    public java.util.List findByName(final int begin, final int limit, final String processName) {
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                StringBuilder buf = new StringBuilder();
                buf.append("FROM neo.workforce.dao.Process o");
                if (processName != null) {
                    buf.append(" WHERE o.name like '%").append(processName).append("%' desc o.createDate");
                }
                log.info(buf.toString());
                Query q = session.createQuery(buf.toString());
                q.setFirstResult(begin);
                q.setMaxResults(limit);
                return q.list();
            }
        });
    }

    public boolean remove(long id) {
        try {
            neo.workforce.dao.Process process = find(id);
            if (process != null) {
                this.getHibernateTemplate().delete(process);
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
}
